home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Environments / Clean 1.2.4 / Small Demos / lqueen.icl < prev    next >
Encoding:
Text File  |  1996-01-17  |  1.3 KB  |  53 lines  |  [TEXT/3PRM]

  1. module lqueen
  2.  
  3. //    The Queens Problem, slow version.
  4.  
  5. import StdEnv
  6.     
  7. BoardSize :== 8 // The size of the chessboard.
  8.  
  9. //    Finding all solutions for the queens problem.
  10.     
  11. Queens::Int [Int] [[Int]] -> [[Int]]
  12. Queens row board boards
  13.     | row>BoardSize    = [board:boards]
  14.     | otherwise        = TryCols BoardSize row board boards
  15.  
  16. //    The second alternative of TryCols is added to make sure Save is never
  17. //    called with an empty list.
  18.     
  19. TryCols::Int Int [Int] [[Int]] -> [[Int]]
  20. TryCols 0 row board boards    =     boards
  21. TryCols col row [] boards    =     TryCols (col-1) row [] queens
  22. where 
  23.     queens    = Queens (row+1) [col] boards
  24.         
  25. TryCols col row board boards
  26.     | Save col 1 board    =     TryCols (col-1) row board queens                     
  27.     | otherwise            =     TryCols (col-1) row board boards
  28. where 
  29.     queens    = Queens (row+1) [col : board] boards
  30.         
  31.  
  32. Save::Int Int [Int] -> Bool
  33. Save c1 rdiff [c2]    =  cdiff<>0 && cdiff<>rdiff && cdiff<> 0 - rdiff
  34. where
  35.     cdiff = c1 - c2
  36.         
  37. Save c1 rdiff [c2:cols]    
  38.     | cdiff==rdiff || cdiff==0 || cdiff==0-rdiff    =     False
  39.     | otherwise                                        =     Save c1 (inc rdiff) cols
  40. where 
  41.     cdiff = c1 - c2
  42.         
  43.  
  44. /*    The Start Rule: Calculate the list of solutions, show the first
  45.     solution and the length of that list.
  46. */
  47.  
  48. Start::(Int,[Int])
  49. Start = (length solutions, hd solutions)
  50.     where 
  51.         solutions = Queens 1 [] []
  52.                 
  53.